/*
 * Bootstrap 3.3.6 IconPicker - jQuery plugin for Icon selection
 *
 * Copyright (c) 20013 A. K. M. Rezaul Karim<titosust@gmail.com>
 * Modifications (c) 20015 Paden Clayton<fasttracksites.com>
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Project home:
 *   https://github.com/titosust/Bootstrap-icon-picker
 *
 * Version:  1.0.1
 *
 */
(function($) {

    $.fn.imageDynamicPicker = function( options ) {

        var mouseOver=false;
        var $popup=null;
        var settings = $.extend({

        }, options);
        return this.each( function() {
            element=this;
            if(!settings.buttonOnly && $(this).data("imageDynamicPicker")===undefined ){
                $this=$(this).addClass("form-control");
                $wraper=$("<div/>",{class:"input-group"});
                $this.wrap($wraper);
                $button=$("<span class=\"input-group-addon pointer\"><i class=\"glyphicon  glyphicon-picture\"></i></span>");
                $this.after($button);
                (function(ele){
                    $button.click(function(){
                        createUI(ele);
                        initSelectDataGrid(ele);
                    });
                })($this);

                $(this).data("imageDynamicPicker",{attached:true});
            }

            function createUI($element){
                $popup=$('<div/>',{
                    css: {
                        'top':$element.offsetParent().position().top+$element.offsetParent().height()+3,
                        'left':0
                    },
                    class:'image-dynamic-picker-popup col-xs-12'
                })

                $popup.html('<div class="row">\
                                <div class="col-xs-12">\
                                    <div class="box">\
                                        <div class="box-body">\
                                            <div class="clearfix">\
                                                <form id="select-data-table-search-form" class="">\
                                                    <div class="col-md-6">\
                                                        <div class="input-group">\
                                                            <span class="input-group-addon"><i class="fa fa-search"></i></span>\
                                                            <input type="text" class="form-control" name="title" placeholder="标题...">\
                                                        </div>\
                                                    </div>\ \
                                                    <div class="col-md-1">\
                                                        <button class="btn btn-default btn-refresh" type="button" name="refresh" title="Refresh"><i class="glyphicon glyphicon-refresh icon-refresh"></i>查询</button>\
                                                    </div>\
                                                </form>\
                                            </div>\
                                            <div class="clearfix" style="padding-top: 15px;">\
                                                <table class="table table-bordered table-striped">\
                                                </table>\
                                            </div>\
                                        </div>\
                                    </div>\
                                </div>\
                            </div>').appendTo($element.parent());

                $popup.addClass('dropdown-menu').show();
                $popup.mouseenter(function() {  mouseOver=true;  }).mouseleave(function() { mouseOver=false;  });

                $(document).mouseup(function (e){
                    if (!$popup.is(e.target) && $popup.has(e.target).length === 0) {
                        removeInstance();
                    }
                });

                $popup.find('.btn-refresh').on('click',function(e){
                    $popup.find(".table").bootstrapTable('refresh');
                });

            }
            function removeInstance(){
                $(".image-dynamic-picker-popup").remove();
            }

            function initSelectDataGrid($element){
                var $selectFileDataGrid = $(".image-dynamic-picker-popup .table");
                $selectFileDataGrid.bootstrapTable({
                    idField : "id",
                    singleSelect: true,
                    onCheck: function (row) {
                        $element.val(row.url);
                        return false;
                    },
                    columns : [[
                        {title: "", field: "checkbox" ,checkbox:true},
                        {
                            title: "文件标题",
                            field: "title" ,
                            formatter: function (value, row, index) {
                                if(row.title){
                                    return '<a href="'+row.urlFull+'" target="_blank"><img style="max-width: 300px;max-height: 150px;"   title="'+row.title+'" src="'+row.urlFull+'" target="_blank" /></a>';
                                }else {
                                    return '<a href="'+row.urlFull+'" target="_blank"><img style="max-width: 300px;max-height: 150px;"  title="" src="'+row.urlFull+'" target="_blank" /></a>';
                                }
                            }
                        },
                        {title: "文件大小", field: "size" ,formatter:function (value, row, index) {
                            return row.size +" K";
                        }}
                    ]],
                    url : '/system/config/file/grid',
                    queryParams: function(files){
                        var searchForm = $("#select-data-table-search-form");
                        var formData = searchForm.serializeObject();
                        if(files.sort){
                            formData["sort"]=files.sort;
                            formData["order"]=files.order;
                        }
                        formData["offset"]=files.offset;
                        formData["limit"]=files.limit;

                        return formData;
                    },
                    responseHandler : function (res) {
                        return {
                            rows : res.rows,
                            total : res.total
                        }
                    },
                    search : false,
                    searchOnEnterKey : false,
                    sortName : 'createTime',
                    sortOrder : 'asc',
                    pagination : true,
                    sidePagination : 'server',
                    pageSize: 10,
                    pageList : [10,20, 40, 50, 100],
                    showRefresh : false,
                    showToggle : false
                });
            }

        });
    }

}(jQuery));